package domain

import (
	"album/dao"
	"album/internal/database"
	"album/model"
	"album/repo"
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"time"
)

type ProcessDomain struct {
	processRepo repo.UpdateListenRepo
}

func NewProcessDomain(mongoClient *database.MongoClient) *ProcessDomain {
	return &ProcessDomain{
		processRepo: dao.NewProcessDao(mongoClient.Db),
	}
}
func (p *ProcessDomain) Create(ctx context.Context, userId, albumId int64, trackId int64, BreakSecond float32) error {
	pro := model.Process{
		UserId:      userId,
		AlbumId:     albumId,
		TrackId:     trackId,
		BreakSecond: BreakSecond,
		IsShow:      1,
		CreateTime:  time.Now().Unix(),
		UpdateTime:  time.Now().Unix(),
	}
	err := p.processRepo.Create(ctx, &pro)
	if err != nil {
		logx.Errorf("mongo 创建失败error: %v", err)
		return err
	}
	return nil
}
func (p *ProcessDomain) Update(ctx context.Context, userId, albumId int64, trackId int64, breakSecond float32) error {
	err := p.processRepo.Update(ctx, userId, albumId, trackId, breakSecond)
	if err != nil {
		logx.Errorf("mongodb 内部更新错误 error: %v", err)
		return err
	}
	return nil
}
func (p *ProcessDomain) FindProcess(ctx context.Context, userId, AlbumId int64, TrackId int64) (*model.Process, error) {
	process, err := p.processRepo.FindProcess(ctx, userId, AlbumId, TrackId)
	if err != nil {
		logx.Errorf("mongodb 内部查询错误 error: %v", err)
		return nil, nil
	}
	return process, nil
}
func (p *ProcessDomain) FindProcessTo1(ctx context.Context, userId int64) (*model.Process, error) {
	process, err := p.processRepo.FindProcessTo1(ctx, userId)
	if err != nil {
		logx.Errorf("mongodb 内部查询错误 error: %v", err)
		return nil, nil
	}
	return process, nil
}
